package com.zjw.service.impl;

import com.zjw.dao.impl.GoodsDaoImpl;
import com.zjw.entity.Goods;
import com.zjw.dao.GoodsDao;
import com.zjw.service.GoodsService;
import com.zjw.utils.ConnectionUtil;
import com.zjw.vo.GoodsDetail;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 好物信息表(Goods)表服务实现类
 *
 * @author 翟
 * @since 2025-04-09 08:59:18
 */
public class GoodsServiceImpl implements GoodsService {

    @Override
    public List<Goods> queryGoodsByLoginTypeAndPaging(int pageNo, int pageSize, String loginType) {
        Connection connection = ConnectionUtil.getConnection();
        GoodsDao goodsDao = new GoodsDaoImpl(connection);
        try {
            int index = (pageNo - 1) * pageSize;
            return goodsDao.selectGoodsByLoginTypeAndPaging(index , pageSize , loginType);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeALL(null , null , connection);
        }
        return null;
    }

    @Override
    public int queryGoodsCountByLoginType(String loginType) {
        Connection connection = ConnectionUtil.getConnection();
        GoodsDao goodsDao = new GoodsDaoImpl(connection);
        try {
            return goodsDao.selectGoodsCountByLoginType(loginType);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeALL(null , null , connection);
        }
        return 0;
    }

    @Override
    public GoodsDetail queryGoodsDetailById(int id, String loginType) {
        Connection connection = ConnectionUtil.getConnection();
        GoodsDao goodsDao = new GoodsDaoImpl(connection);
        try {
            return goodsDao.selectGoodsDetailByGoodsId(id , loginType);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeALL(null , null , connection);
        }
        return null;
    }

    @Override
    public boolean addGoods(Goods goods) {
        Connection conn = ConnectionUtil.getConnection();
        GoodsDao goodsDao = new GoodsDaoImpl(conn);
        try {
            return goodsDao.insertGoods(goods) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeALL(null , null , conn);
        }
        return false;
    }

    @Override
    public List<Goods> queryGoodsByUserIdAndPaging(int currPageNo, int pageSize, int userId) {
        Connection conn = ConnectionUtil.getConnection();
        GoodsDao goodsDao = new GoodsDaoImpl(conn);
        int index = (currPageNo - 1) * pageSize;
        try {
            return goodsDao.selectGoodsByUserIdAndPaging(index, pageSize, userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeALL(null , null , conn);
        }
        return null;
    }

    @Override
    public int queryGoodsCountByUserId(int userId) {
        Connection conn = ConnectionUtil.getConnection();
        GoodsDao goodsDao = new GoodsDaoImpl(conn);
        try {
            return goodsDao.selectGoodsCountByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeALL(null , null , conn);
        }
        return 0;
    }
}
